package com.mucfc.crm;


import com.mucfc.crm.common.utils.FileUtil;
import com.mucfc.crm.common.utils.ZipUtil;
import com.mucfc.nty.dto.EmailAttachDto;
import com.mucfc.nty.dto.SendEmailRequest;
import com.mucfc.nty.service.IEmailSendService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class MucfcEmailClient {
    private static Logger log = LoggerFactory.getLogger(MucfcEmailClient.class);

    private static ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[]{"moa-dubbo-consumer.xml"});

    @Test
    public void sendEmail(String mailTo, String mailCc, String mailBcc, String subject, String content,
                          List<File> attachList, String sys) throws IOException {

        IEmailSendService emailServcie = (IEmailSendService) context.getBean("emailService");

        SendEmailRequest request = new SendEmailRequest();

        request.setEmail(mailTo);

        if (mailCc != null && !"".equals(mailCc.trim())) {
            request.setEmailCc(mailCc);
        }

        if (mailBcc != null && !"".equals(mailBcc.trim())) {
            request.setEmailBcc(mailBcc);
        }

        request.setSubject(subject);
        request.setContent(content);

        request.setFlowNo((new Date()).getTime() + "_" + UUID.randomUUID().toString().replace("-", ""));
        request.setOperSys(sys);

        List<EmailAttachDto> attachDtoList = new ArrayList<EmailAttachDto>();
        List<File> zipFileList = new ArrayList<File>();
        for (File f : attachList) {
            if (!f.exists()) {
                continue;
            }

            EmailAttachDto attachDto = new EmailAttachDto();

            // 如果附件大于2M，就压缩
            final int maxSize = 2;
            if (f.length() > 1024 * 1024 * maxSize) {
                File zipFile = ZipUtil.zip(f);
                log.info(">>附件\"{}\"过大，对附件进行压缩后得到：\"{}\"", f.getName(), zipFile.getCanonicalPath());
                zipFileList.add(zipFile);

                attachDto.setFileFullName(zipFile.getName());
                attachDto.setFileData(FileUtil.getBytes(zipFile));
                log.info(">>成功将\"{}\"粘贴到邮件中！", zipFile.getName());
            } else {
                attachDto.setFileFullName(f.getName());
                attachDto.setFileData(FileUtil.getBytes(f));
                log.info(">>成功将\"{}\"粘贴到邮件中！", f.getName());
            }

            attachDtoList.add(attachDto);
        }
        if (!attachDtoList.isEmpty()) {
            log.info(">>附件个数： " + attachDtoList.size());
            request.setAttachList(attachDtoList);
        }


        // 调用邮件接口
        log.info(">>正在发送邮件...");
        emailServcie.sendEmail(request);
        log.info(">>发送邮件成功！");

        // 删除压缩文件
        for (File f : zipFileList) {
            f.delete();
        }
        zipFileList.clear();
    }


    public static void main(String[] args) {
        try {
            String mailTo = "443770574@qq.com";
            String mailCc = "huqingmiao@mucfc.com";
            String mailBcc = "443770574@qq.com";
            String subject = "这里放主题";
            String content = "Hello, 美国好！";
            String operator = "RPT";

            List<File> files = new ArrayList<File>();
            files.add(new File("d:/跟踪事项.xls"));
            files.add(new File("d:/a.xls"));
            files.add(new File("d:/工银关贸平台概要设计说明书.doc"));

            MucfcEmailClient app = new MucfcEmailClient();
            app.sendEmail(mailTo, mailCc, mailBcc, subject, content, files, operator);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
